package com.netease.yunxin.app.im.welcome

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import com.google.gson.Gson
import com.net.PolizeApi
import com.net.PostReg
import com.net.RetrofitManager
import com.net.RxCompat
import com.netease.yunxin.app.im.R
import com.netease.yunxin.kit.common.ui.activities.BaseActivity
import kt.toast
import okhttp3.FormBody
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import retrofit2.http.Field

class RegisterActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        findViewById<ImageView>(R.id.ivBack).setOnClickListener {
            finish()
        }
        findViewById<Button>(R.id.btnRegister).setOnClickListener {
            toRegister()
        }
        val etUsername = findViewById<EditText>(R.id.etUsername)
        val etPassword = findViewById<EditText>(R.id.etPassword)
//        etUsername.setText("18688367977")
//        etPassword.setText("111111")
    }

    private fun toRegister() {
        val etUsername = findViewById<EditText>(R.id.etUsername)
        val etPassword = findViewById<EditText>(R.id.etPassword)

        val username = etUsername.text.toString().trim()
        val password = etPassword.text.toString().trim()
        val postReg = PostReg(username, username, password)
        val body =
            Gson().toJson(postReg).toRequestBody("application/json; charset=utf-8".toMediaType())

        RetrofitManager.of(PolizeApi::class.java).regsiter(body)
            .compose(RxCompat.schedulersTransformer()).subscribe({ ret ->
                if (ret.code == 200) {
                    toast("注册成功")
                    finish()
                } else {
                    toast("注册失败,${ret.msg}")
                }
            }, {
                toast("注册失败，错误信息：$it")
            })
//            {"msg":"注册成功","code":200,"data":"9cdd73298477777781f006d3dd351729"}
    }

    override fun finish() {
        super.finish()
    }
}